/*
 * usart.cpp
 *
 *  Created on: 07.02.2012
 *      Author: Roma Jam
 */

#include "stm32f10x_usart.h"
#include "stm32f10x_gpio.h"
#include "usart.h"
#include "delay_util.h"

usart_t Usart;

void usart_t::Init(void) {
    // ==== Clocks init ====
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1, ENABLE);
    // ==== GPIO init ====
    GPIO_InitTypeDef GPIO_InitStructure;
    // Configure USART Tx as alternate function push-pull
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Pin   = USART1_TX_PIN;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(USART1_GPIO, &GPIO_InitStructure);
    // Configure USART Rx as input floating
//    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_InitStructure.GPIO_Pin = USART1_RX_PIN;
    GPIO_Init(USART1_GPIO, &GPIO_InitStructure);

    // ==== USART configuration ====
    USART_InitTypeDef USART_InitStructure;
    USART_InitStructure.USART_BaudRate = 115200;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init(USART1, &USART_InitStructure);
    // Enable USART
    USART_Cmd(USART1, ENABLE);
} // Init

void usart_t::Task(void) {
    if (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET) return;
    uint8_t b = USART_ReceiveData(USART2);
    Print(b);
}
void usart_t::Send (uint16_t AByte) {
    USART_SendData(USART1, AByte);
    while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
void usart_t::Print (uint8_t AByte) {
    // Write a character to the USART
    USART_SendData(USART2, AByte);
    // Loop until the end of transmission
    while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
} // Print
void usart_t::Print_Char (uint8_t DChar) {
    uint8_t byte = DChar;
    Print(byte);
} // Print_Char
void usart_t::Print_String (const char *R) {
    while (*R != '\0')
    Print(*R++);
}
void usart_t::Print_Uint(uint16_t ANumber) {
    uint8_t digit = '0';
        bool ShouldPrint = false;
        // >10000
        while (ANumber >= 10000) {
            digit++;
            ANumber -= 10000;
        }
        if (digit != '0'){
            Print(digit);
            ShouldPrint = true;
        }
        digit = '0';
        // >1000
        while (ANumber >= 1000){
            digit++;
            ANumber -= 1000;
        }
        if ((digit != '0') || ShouldPrint) {
            Print(digit);
            ShouldPrint = true;
        }
        digit = '0';

        // > 100
        while (ANumber >= 100) {  // Still larger than 100 ?
            digit++;   // Increment first digit
            ANumber -= 100;
        }
        if ((digit != '0') || ShouldPrint) {
                Print(digit);
                ShouldPrint = true;
        }
        digit = '0';
        // > 10
        while (ANumber >= 10) {  // Still larger than 10 ?
            digit++;   // Increment second digit
            ANumber -= 10;
        }
        Print(digit);
        Print('0'+ANumber);
} // Print_Uint
